package com.jxmcloud.business.cut.controller.nah.login;

import cn.hutool.core.util.ObjectUtil;
import com.jxmcloud.business.ce.service.CeSysParameterService;
import com.jxmcloud.business.cut.constants.CutConstants;
import com.jxmcloud.business.cut.constants.CutRedisConstant;
import com.jxmcloud.business.cut.dto.gm.AccountItemDto;
import com.jxmcloud.business.cut.dtoCommon.BaseDto;
import com.jxmcloud.business.cut.json.DataConfigService;
import com.jxmcloud.business.cut.model.database.DBUserLoginInfo;
import com.jxmcloud.business.cut.service.app.system.ISystemService;
import com.jxmcloud.business.cut.service.user.IUserBagService;
import com.jxmcloud.business.cut.service.user.IUserLoginInfoService;
import com.jxmcloud.business.cut.service.user.IUserManagerService;
import com.jxmcloud.business.cut.util.RedissonLockUtil;
import com.jxmcloud.business.cut.vo.ForwardIndexDto;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.xlcloud.platform.api.exception.BusinessException;
import com.xlcloud.platform.util.PublicUtil;
import com.xlcloud.platform.util.RUtil;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;


/**
 * App 用户信息请求
 *
 * @author fengc 2025-06-05 10:02:54
 */
@Controller
@RequestMapping("/nah/login/AppController")
@RestController
public class AppController {
    @Autowired
    private CeSysParameterService sysParameterService;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private IUserManagerService userManagerService;

    @Autowired
    private DataConfigService dataConfigService;

    @Autowired
    private ISystemService systemService;

    @Autowired
    private IUserBagService userBagService;

    @Autowired
    private IUserLoginInfoService userLoginInfoService;

    /**
     * 获取用户信息
     * @param sessionDto
     * @return
     */
    @RequestMapping("/getUserInfo")
    public String getUserInfo(@RequestBody BaseDto sessionDto) {
        return RUtil.appSuccess(userManagerService.getUserInfo(sessionDto.getSession()));
    }

    /**
     * 玄乐配置：跳转到砍一刀服务首页
     * @return
     */
    @GetMapping("/forwardPddIndex")
    public String forwardPddIndex() {
        String url = sysParameterService.getParamByKey(CutConstants.FORWARD_PDD_INDEX).getParameterValue();
        ForwardIndexDto forwardIndexDto = ForwardIndexDto.builder()
                .indexUrl(url)
                .build();
        return RUtil.appSuccess(forwardIndexDto);
    }

    /**
     * 添加道具
     * @param accountItemDto
     * @return
     */
    @PostMapping("/addBag")
    public String addBag(@RequestBody AccountItemDto accountItemDto) {
        DBUserLoginInfo one = this.userLoginInfoService.lambdaQuery()
                .eq(DBUserLoginInfo::getAccount, accountItemDto.getAccount())  // 根据账号字段精确查询
                .one();
        PlayerInfoVo player = userManagerService.getUserInfo(one.getSessionKey());
        if (ObjectUtil.isNull(player)) { throw new BusinessException("请先登录");}
        // 批量添加道具
        userBagService.batchAddItem(player,accountItemDto);
        // 刷新缓存信息
        userManagerService.refreshPlayerCache(player);
        return RUtil.appSuccess("成功");
    }

    /**
     * 重新加载数据配置成功
     * @return
     */
    @PostMapping("/reloadDataConfig")
    public String reloadDataConfig() {
        new RedissonLockUtil(redissonClient, CutRedisConstant.RELOAD_DATA_CONFIG, 5, TimeUnit.SECONDS)
                .lock(() -> systemService.reloadServiceConfig());
        return RUtil.appSuccess("重新加载数据配置成功");
    }

    /**
     * 清除用户活动数据
     * @return
     */
    @GetMapping("/clearUser")
    public String clearUser(Long account) {
        if(account == null){
            return RUtil.appFail("用户账号不能为NULL");
        }
        new RedissonLockUtil(redissonClient, CutRedisConstant.RELOAD_DATA_CONFIG, 5, TimeUnit.SECONDS)
                .lock(() -> systemService.clearUserActivity(account));
        return RUtil.appSuccess("clearUser数据成功");
    }


    @PostMapping({"/getSysparameter"})
    public String getAppParamByKeyPost(String parameterKey) {
        return PublicUtil.isEmpty(parameterKey) ? RUtil.fail("请传入正确参数") : RUtil.success(this.sysParameterService.getParamByKey(parameterKey));
    }
}
